<?php
include_once "Distributor.php";

class DistributorDAO {
	
	public $link;
	public static $instance;
	
	
	public function __construct() {
		$this->link = mysql_connect("localhost:3306", "root", "");
		mysql_select_db("ssregistration") or die(mysql_error());
	}
	
	public static function getInstance() {
		if(!self::$instance) {
			self::$instance = new self();
		}
		
		return self::$instance;
	}
	
	public function searchDistributorByID($distID) {
		$result = mysql_query("SELECT * FROM dist_list WHERE dist_id = '$distID'") or die(mysql_error());
		
		$distList = array();
		
		while($row = mysql_fetch_array($result)) {
			$dist = new Distributor($row['pack_no'], $row['dist_id'], $row['name'], $row['assoc1'], $row['assoc2'], $row['assoc3'], $row['pax'], $row['pin_title'], $row['attendance']);
			$distList[] = $dist;
		}
		
		return $distList;
	}
	
	public function searchDistributorByName($name) {
		$result = mysql_query("SELECT * FROM dist_list WHERE name LIKE '%$name%' OR assoc1 LIKE '%$name%' OR assoc2 LIKE '%$name%' OR assoc3 LIKE '%$name%'");
		
		$distList = array();
		
		while($row = mysql_fetch_array($result)) {
			$dist = new Distributor($row['pack_no'], $row['dist_id'], $row['name'], $row['assoc1'], $row['assoc2'], $row['assoc3'], $row['pax'], $row['pin_title'], $row['attendance']);
			$distList[] = $dist;
		}
		
		return $distList;
	}
	
	public function setCollected($distID) {
		mysql_query("UPDATE dist_list SET attendance='Y' WHERE dist_id = '$distID'");
	}
	
	public function setUnCollected($distID) {
		mysql_query("UPDATE dist_list SET attendance='N' WHERE dist_id = '$distID'");
	}
	
	public function selectDistributorByPinTitle($pinTitle, $collectionStatus) {
	
	}
	
	public function insertRecord(Distributor $dist){	
		
		$packNo = $dist->getPackNo();
		$distId = $dist->getDistID();
		$name = mysql_real_escape_string($dist->getName());
		$assoc1 = mysql_real_escape_string($dist->getAssoc1());
		$assoc2 = mysql_real_escape_string($dist->getAssoc2());
		$assoc3 = mysql_real_escape_string($dist->getAssoc3());
		$pax = $dist->getPax();
		$pinTitle = $dist->getPinTitle();
		$attendance = $dist->getAttendance();
		
		mysql_query("INSERT INTO dist_list (pack_no, dist_id, name, assoc1, assoc2, assoc3, pax, pin_title, attendance) VALUES ('$packNo', '$distId', '$name', '$assoc1', '$assoc2', '$assoc3', '$pax', '$pinTitle', '$attendance')") or die(mysql_error());	

	}
	
	public function downloadFile() {
		
		$result = mysql_query('SELECT * FROM dist_list');
		
		if (!$result) die('Couldn\'t fetch records');
		
		$num_fields = mysql_num_fields($result);
		$headers = array("1"=>"S/N", "2"=>"MEM ID", "3"=>"NAME", "4"=>"ASSOC1", "5"=>"ASSOC2", "6"=>"ASSOC3", "7"=>"No. OF PAX", "8"=>"PIN TITLE", "9"=>"ATTENDANCE(Y/N)" );
		
	/*	
		for ($i = 0; $i < $num_fields; $i++) {
    		$headers[] = mysql_field_name($result , $i);
		}
	*/
		$fp = fopen('php://output', 'w');
		if ($fp && $result) {
		    header('Content-Type: text/csv');
		    header('Content-Disposition: attachment; filename="export.csv"');
		    header('Pragma: no-cache');
		    header('Expires: 0');
		    fputcsv($fp, $headers);
		    while ($row = mysql_fetch_array($result)) {
		//        fputcsv($fp, array_values($row));
				$data = array ("1"=>$row['pack_no'], "2"=>$row['dist_id'], "3"=>$row['name'], "4"=>$row['assoc1'], "5"=>$row['assoc2'], "6"=>$row['assoc3'] , "7"=>$row['pax'], "8"=>$row['pin_title'], "9"=>$row['attendance']);
				fputcsv($fp, $data, ",");
		    }
		    die;
}
	}
	
	public function close() {
		mysql_close();
	}

	
}
?>